﻿using Cinema_Ticketing_System.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Cinema_Ticketing_System.Controllers
{
    public class HomePageController : Controller
    {
        Cinema_Area cinema_Area = new Cinema_Area();
        Cinema_Name cinema_Name = new Cinema_Name();
        Show_film show_Film = new Show_film();
        Unreleased_Film unreleased_Film = new Unreleased_Film();
        Area_Infromation area_Infromation = new Area_Infromation();
        Get_Play get_Play = new Get_Play();
        Get_Play_time get_Play_Time = new Get_Play_time();
        // GET: HomePage
        public ActionResult HomePage(int area=-99,int pageIndex=1,int requestStatus=0, string keywords = null, int cid = -99, int fid = -88)
        {
            var x = -100;
            var listarea = cinema_Area.GetArea();
            ViewBag.lsitarea = listarea;
            var listname = cinema_Name.GetCinemaName(area,keywords,x);
            ViewBag.listname = listname;


            //获取上映的电影
            var listshowfilm = show_Film.GetShowFilm(fid);
            int pagenum= (int)Math.Ceiling(Convert.ToDouble(listshowfilm.Count) / 6);
            var listPage = listshowfilm.Skip((pageIndex - 1) * 6).Take(6);

            //获取未上映的电影
            var listunreleasedfilm = unreleased_Film.GetUnreleasedFilm(fid);
            int pagenum2 = (int)Math.Ceiling(Convert.ToDouble(listunreleasedfilm.Count) / 6);
            var listPage2 = listunreleasedfilm.Skip((pageIndex - 1) * 6).Take(6);

            

            if (requestStatus == 0)
            {
                ViewBag.listshowfilm = listPage;
                ViewBag.listunreleasedfilm = listPage2;
                ViewBag.pageNum = pagenum;
                ViewBag.pageNum2 = pagenum2;
                return View();
            }
            else 
            {
                if (requestStatus == 1)
                {
                    var list = from s in listPage
                               select new { s.cover, s.filmname };


                    JsonSerializerSettings settings = new JsonSerializerSettings();
                    string jsongStr = JsonConvert.SerializeObject(list, settings);

                    return Content(jsongStr, "application/json");
                }
                else if (requestStatus == 5)
                {
                    var list = from s in listname
                               select new { s.cinemaid, s.cinemaname };


                    JsonSerializerSettings settings = new JsonSerializerSettings();
                    string jsongStr = JsonConvert.SerializeObject(list, settings);

                    return Content(jsongStr, "application/json");
                }
                else
                {
                    var list = from s in listPage2
                               select new { s.cover, s.filmname };


                    JsonSerializerSettings settings = new JsonSerializerSettings();
                    string jsongStr = JsonConvert.SerializeObject(list, settings);

                    return Content(jsongStr, "application/json");
                }
                
            }

            

        }

        public ActionResult FilmInformation(int pageIndex = 1, int requestStatus = 0, int cid = -100, int fid = -88)
        {
            var x = -88;
            if (fid != -88)
            {
                Session["fid"] = fid;
            }

            var playlist = get_Play.GetPlay(cid, fid);
            if (playlist != null)
            {
                foreach (var i in playlist)
                {
                    x = (int)i.filmid;
                }
            }
            //获取上映的电影
            var listshowfilm = show_Film.GetShowFilm(fid);
            int pagenum = (int)Math.Ceiling(Convert.ToDouble(listshowfilm.Count) / 6);
            var listPage = listshowfilm.Skip((pageIndex - 1) * 6).Take(6);

            //获取未上映的电影
            var listunreleasedfilm = unreleased_Film.GetUnreleasedFilm(fid);
            int pagenum2 = (int)Math.Ceiling(Convert.ToDouble(listunreleasedfilm.Count) / 6);
            var listPage2 = listunreleasedfilm.Skip((pageIndex - 1) * 6).Take(6);



            if (requestStatus == 0)
            {
                ViewBag.listshowfilm = listPage;
                ViewBag.listunreleasedfilm = listPage2;
                ViewBag.pageNum = pagenum;
                ViewBag.pageNum2 = pagenum2;
                return View();
            }
            else
            {
                if (requestStatus == 1)
                {
                    var list = from s in listPage
                               select new { s.cover, s.filmname ,s.brief_introduction,s.typeid,s.star,s.duration};


                    JsonSerializerSettings settings = new JsonSerializerSettings();
                    string jsongStr = JsonConvert.SerializeObject(list, settings);

                    return Content(jsongStr, "application/json");
                }
                else
                {
                    var list = from s in listPage2
                               select new { s.cover, s.filmname, s.brief_introduction, s.typeid, s.star, s.duration };


                    JsonSerializerSettings settings = new JsonSerializerSettings();
                    string jsongStr = JsonConvert.SerializeObject(list, settings);

                    return Content(jsongStr, "application/json");
                }

            }
        }


        public ActionResult CinemaInformation(int area = -99, int pageIndex = 1, int requestStatus = 0,string keywords=null ,int cid=-99 ,int fid=-88)
        {
            var x=-100;
            if (cid != -99)
            {
                Session["cid"] = cid;
            }

            var playlist = get_Play.GetPlay(cid,fid);
            if(playlist!=null){
                foreach (var i in playlist)
                {
                    x = (int)i.cinemaid;
                }
            }
            

            var listarea = area_Infromation.GetAreaname();
            ViewBag.listarea = listarea;

            var listname = cinema_Name.GetCinemaName(area,keywords,x);
            int pagenum = (int)Math.Ceiling(Convert.ToDouble(listname.Count) / 6);
            var listPage = listname.Skip((pageIndex - 1) * 6).Take(6);

            if (requestStatus == 0)
            {
                ViewBag.listname = listPage;
                ViewBag.pageNum = pagenum;
                return View();
            }
            else if(requestStatus == 1)
            {
                var list = from s in listPage
                           select new { s.cinemaid, s.cinemaname, s.cinemascore, s.contact_information, s.cinemaaddress,s.cinemaurl};


                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(list, settings);

                return Content(jsongStr, "application/json");

            }
            else
            {
                var list = from s in listPage
                           select new { s.cinemaid, s.cinemaname, s.cinemascore, s.contact_information, s.cinemaaddress,s.cinemaurl };

                JsonSerializerSettings settings = new JsonSerializerSettings();

                var listPageNum = new { pagenum, list };
                string jsongStr = JsonConvert.SerializeObject(listPageNum, settings);
                return Content(jsongStr, "application/json");
            }

        }

        public ActionResult Buying_Tickets(int area = -99, string keywords = null)
        {
            var cid =(int)Session["cid"];
            var fid = (int)Session["fid"];
            if (fid == -88)
            {
                return RedirectToAction("FilmInformation", "HomePage"); 
            }
            if (cid == -99)
            {
                return RedirectToAction("CinemaInformation", "HomePage");
            }

            var listplaytime = get_Play_Time.GetPlaytime(cid, fid);

            ViewBag.listplaytime = listplaytime;
            var listname = cinema_Name.GetCinemaName(area, keywords,cid);
            ViewBag.listname = listname;
            
            return View();
        }
    }
}